package com.example.makerspace;

import com.example.makerspace.utils.HttpClientUtil;
import com.example.makerspace.utils.MD5Utils;
import java.util.Date;
public class identityCheck {
    public static boolean identityCheck(String token,String timeStamp){
        System.out.println("-----------------开始调用--------------->");
        System.out.println("token"+token);
        String key = "263d5d33**b54b2da7d7a2742fc17828";
        String keyword = "阿里巴巴";
        //企业关键字模糊查询
        String url = "http://api.qichacha.com/ECIV4/Search?key=" + key + "&dtype=json" + "&keyword=" + keyword;
        //企业关键字获取详细信息
//        String url = "http://api.qichacha.com/ECIV4/GetBasicDetailsByName?key=" + key + "&dtype=json" + "&keyword=" + keyword;
        System.out.println("请求url:" + url);
        boolean match = false; //是否匹配
        try {
            String result = HttpClientUtil.doGet(url,token,timeStamp);
            System.out.println("请求结果：" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("<-----------------调用结束---------------");
        return match;
    }

    public static void main(String[] args) throws Exception{
        String key = "263d5d338cb54b2d**d7a2742fc17828";
        int secondTimestamp = getSecondTimestamp(new Date()); //精确到秒的时间戳
        String secretKey ="B24B271A56A3**F16B6B73FFF64D80B2"; //密钥
        String token = MD5Utils.MD5(key+secondTimestamp+secretKey); //token:验证加密值（key+Timespan+SecretKey组成的32位md5加密的大写字符串）
        System.out.println(token);
        identityCheck(token,secondTimestamp+"");
    }

    /**
     * 获取精确到秒的时间戳
     * @return
     */
    public static int getSecondTimestamp(Date date){
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime());
        int length = timestamp.length();
        if (length > 3) {
            return Integer.valueOf(timestamp.substring(0,length-3));
        } else {
            return 0;
        }
    }
}